package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.constant.ConfigConstants;
import cn.sfrog.common.exception.ServiceException;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.domain.BizRechargeUnit;
import cn.sfrog.project.biz.admin.domain.vo.BizRechargeUnitFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizRechargeUnitQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizRechargeUnitVO;
import cn.sfrog.project.biz.admin.mapper.BizRechargeUnitMapper;
import cn.sfrog.project.biz.admin.service.IBizRechargeUnitService;
import cn.sfrog.system.domain.SysConfig;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.mapper.SysConfigMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 陪护豆充值单位Service业务层处理
 */
@Service
public class BizRechargeUnitServiceImpl extends ServiceImpl<BizRechargeUnitMapper, BizRechargeUnit> implements IBizRechargeUnitService {
    @Resource
    private BizRechargeUnitMapper bizRechargeUnitMapper;
    @Resource
    private SysConfigMapper sysConfigMapper;
    /**
     * 查询陪护豆充值单位
     *
     * @param id 陪护豆充值单位主键
     * @return 陪护豆充值单位
     */
    @Override
    public BizRechargeUnitVO selectBizRechargeUnitById(Long id) {
        return bizRechargeUnitMapper.selectBizRechargeUnitById(id);
    }

    /**
     * 查询陪护豆充值单位列表
     *
     * @param query 陪护豆充值单位
     * @return 陪护豆充值单位
     */
    @Override
    public List<BizRechargeUnitVO> selectBizRechargeUnitList(BizRechargeUnitQueryVO query) {
        return bizRechargeUnitMapper.selectBizRechargeUnitList(query);
    }

    /**
     * 新增陪护豆充值单位
     *
     * @param form 陪护豆充值单位
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizRechargeUnit(BizRechargeUnitFormVO form) {
        LambdaQueryWrapper<BizRechargeUnit> queryWrapper = new LambdaQueryWrapper<BizRechargeUnit>()
                .eq(BizRechargeUnit::getEscortBeanCount,form.getEscortBeanCount());
        BizRechargeUnit dbEntity = BizRechargeUnit.DAO.selectOne(queryWrapper);
        if(dbEntity != null){
            throw new ServiceException("已经存在相同充值数量的数据了");
        }
        BizRechargeUnit bizRechargeUnit = new BizRechargeUnit();
        //陪护豆兑换比例 (1元人民币兑换 {sysConfig.configValue} 个陪护豆)
        SysConfig sysConfig = sysConfigMapper.checkConfigKeyUnique(ConfigConstants.ESCORT_BEAN_EXCHANGE_SCALE);
        String configValue = sysConfig.getConfigValue();
        int price = form.getEscortBeanCount() / Integer.parseInt(configValue);
        BeanUtils.copyProperties(form, bizRechargeUnit);
        bizRechargeUnit.setNeedPrice(BigDecimal.valueOf(price));
        bizRechargeUnit.setCreateTime(new Date());
        bizRechargeUnit.setCreateBy(SecurityUtils.getUsername());
        bizRechargeUnit.setUpdateTime(new Date());
        bizRechargeUnit.setUpdateBy(SecurityUtils.getUsername());
        return bizRechargeUnit.insert();
    }

    /**
     * 修改陪护豆充值单位
     *
     * @param form 陪护豆充值单位
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizRechargeUnit(BizRechargeUnitFormVO form) {
        LambdaQueryWrapper<BizRechargeUnit> queryWrapper = new LambdaQueryWrapper<BizRechargeUnit>()
                .eq(BizRechargeUnit::getEscortBeanCount,form.getEscortBeanCount())
                .ne(BizRechargeUnit::getId,form.getId());
        BizRechargeUnit dbEntity = BizRechargeUnit.DAO.selectOne(queryWrapper);
        if(dbEntity != null){
            throw new ServiceException("已经存在相同充值数量的数据了");
        }
        //陪护豆兑换比例 (1元人民币兑换 {sysConfig.configValue} 个陪护豆)
        SysConfig sysConfig = sysConfigMapper.checkConfigKeyUnique(ConfigConstants.ESCORT_BEAN_EXCHANGE_SCALE);
        String configValue = sysConfig.getConfigValue();
        int price = form.getEscortBeanCount() / Integer.parseInt(configValue);
        BizRechargeUnit bizRechargeUnit = new BizRechargeUnit();
        BeanUtils.copyProperties(form, bizRechargeUnit);
        bizRechargeUnit.setNeedPrice(BigDecimal.valueOf(price));
        bizRechargeUnit.setUpdateTime(new Date());
        bizRechargeUnit.setUpdateBy(SecurityUtils.getUsername());
        return bizRechargeUnit.updateById();
    }

    /**
     * 批量删除陪护豆充值单位
     *
     * @param ids 需要删除的陪护豆充值单位主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizRechargeUnitByIds(Long[] ids) {
            return BizRechargeUnit.DAO.delete(Wrappers.lambdaQuery(BizRechargeUnit.class).in(BizRechargeUnit::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return BizRechargeUnit.DAO.update(Wrappers.lambdaUpdate(BizRechargeUnit.class).in(BizRechargeUnit::getId, (Object[]) vo.getIds()).set(BizRechargeUnit::getStatus, vo.getStatus()));
    }
}
